#include "guass.h"

Guass::Guass(const QString &qs,const int &n)
{
    len = n;
    std::stringstream ss(qs.toStdString());
    try{
        for(int i=0;i<n;i++){
            for(int j=0;j<=n;j++){
                ss >> val[i][j];
            }
        }
    }catch(...){

    }
}
void Guass::Run(){
    for(int i=0;i<len;i++){
            int mVal = i;
            for(int j=i;j<len;j++){
                if(fabs(val[j][i])>fabs(val[mVal][i])){
                    mVal  = j;
                }
            }

            if(i!=mVal){
                for(int j=i;j<=len;j++){
                    std::swap(val[i][j],val[mVal][j]);
                }
            }
            double tval  = val[i][i];
            if(tval){
                for(int j=i;j<=len;j++)
                    val[i][j] = val[i][j]/tval;
                for(int k=i+1;k<len;k++){
                    double tvalVal = val[k][k];
                    for(int j=i;j<=len;j++){
                        val[k][j] = val[k][j] - tvalVal*val[i][j];
                    }
                }
            }
        }

        for(int i=len-1;i>=0;i--){
            double sum = 0;
            for(int j=len-1;j>i;j--){
                sum += ans[j]*val[i][j];
            }
            ans[i] = val[i][len] - sum;
        }
}

QString Guass::getAns(){
    QString rev;
    for(int i=0;i<len;i++){

        rev += QString::number(ans[i]) + '\n';


    }
 //   QMessageBox::information(NULL,"Title",rev);
    return rev;
}
